<?php

class BackMemberController extends BackController {

    static $model_name = 'member';
    static $display_name = '会员';
	/*
	|--------------------------------------------------------------------------
	| Default Home Controller
	|--------------------------------------------------------------------------
	|
	| You may wish to use controllers instead of, or in addition to, Closure
	| based routes. That's great! Here is an example controller method to
	| get you started. To route to this controller, just add the route:
	|
	|	Route::get('/', 'HomeController@showWelcome');
	|
	*/

	public function getIndex()
	{
        $result =  $this->getService()->getList();
        $entries = $result['entries'];
        $links = $result['links'];

        return View::make(self::$view_path.'.'.self::$model_name.'.index', compact('entries', 'links'));
	}


    public function doCreate(){
        $service = $this->getService();
        if($this->isSubmit()){
            if(!$service->createMember()){
                return Redirect::action('BackMemberController@doCreate')->withInput()->with('errors',$this->service->errors());
            }else{
                return Redirect::action('BackMemberController@getIndex')->with('success','创建'.self::$display_name.'成功');
            }
        }
        return View::make(self::$view_path.'.'.self::$model_name.'.create_update');
    }

    public function doUpdate($entry){
        $service = $this->getService();
        if($this->isSubmit()){
            if(!$service->updateMember($entry)){
                return Redirect::action('BackMemberController@doUpdate', array($entry->id))->withInput()->with('errors',$this->service->errors());
            }else{
                return Redirect::action('BackMemberController@doUpdate', array($entry->id))->with('success','修改'.self::$display_name.'成功');
            }
        }
        $accountLogsResult = $service->getAccountLogList($entry);
        $accountLogs = $accountLogsResult['entries'];
        $links = $accountLogsResult['links'];
        return View::make(self::$view_path.'.'.self::$model_name.'.create_update', compact('entry', 'accountLogs', 'links'));
    }

    public function getService() {
        if(is_null($this->service)){
            $this->service = new BackMemberService();
        }
        return $this->service;
    }

    /**
     * Remove the specified user from storage.
     *
     * @param $entry
     * @internal param $id
     * @return Response
     */
    public function doDelete($entry)
    {
        $service = $this->getService();

        if(!$service->deleteMember($entry)) {
            return Redirect::action('BackMemberController@getIndex')->with('error',$this->service->errors()->all());

        }
        return Redirect::action('BackMemberController@getIndex')->with('success', '删除'.self::$display_name.'成功');
    }

}
